package com.msmall.community.convene.service.impl;

import java.util.Date;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;
import com.msmall.community.common.util.FilterCharsUtil;
import com.msmall.community.convene.dao.ConveneDao;
import com.msmall.community.convene.model.Convene;
import com.msmall.community.convene.service.ConveneManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class ConveneManagerImpl extends BaseManagerImpl<Convene, Long> implements ConveneManager{

	private static final Log log = LogFactory.getLog(ConveneManagerImpl.class);
	
	private ConveneDao conveneDao;
	
	private UserManager userManager;

	public void setConveneDao(ConveneDao conveneDao) {
		this.conveneDao = conveneDao;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	/**
	 * 用户删除自己的召集令
	 * @param conveneIds ","分割
	 * @throws Exception
	 */
	public void removeConvene(String conveneIds, Long userid) throws Exception{
		try{
			if(!StringUtils.isBlank(conveneIds)){
				String[] conveneId = conveneIds.split(",");
				for(int i =0 ; i <conveneId.length; i++){
					Long id = Long.valueOf(conveneId[i]);
					Convene convene = (Convene) conveneDao.get(Convene.class, id);
					convene.setEnabled(ConstantDB.UNAVALIABLE);
					conveneDao.saveObject(convene);
				}
			}
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	
	public void saveConvene(Convene convene, Long userid) throws Exception{
		try{
			convene.setCreateTime(new Date());
			if(convene.getEnabled() == null){
				convene.setEnabled(ConstantDB.AVALIABLE);
			}
			UserInfo user = userManager.getByUserid(userid);
			convene.setNickname(user.getNickname());
			convene.setUser(user);
			conveneDao.saveObject(convene);
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	/**
	 * 获得我所有发布的召集令
	 * @param page
	 * @param userid
	 * @throws Exception
	 */
	public void getMyConvenes(PaginationSupport page, Long userid) throws Exception{
		try{
			String queryString = "from Convene c where c.enabled=" + ConstantDB.AVALIABLE +" and c.user.userid="+userid+" order by createTime desc";
			page.setQueryString(queryString);
			getPagination(page);
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	/**
	 * 验证Convene content
	 * @return 1:正常；2字符太少；3字符太多;4含有非法字符
	 */
	public int validateConveneContent(String content) {
		int min = Configuration.getIntegerValue(Configuration.CONVENE_CONTENT_MIN_CHARS);
		int max = Configuration.getIntegerValue(Configuration.CONVENE_CONTENT_MAX_CHARS);
		if(StringUtil.nullOrBlank(content)){
			return 2;
		}
		if(content.length() < min){
			return 2;
		}
		if(content.length() > max){
			return 3;
		}
		boolean titleC = FilterCharsUtil.containsFilterChars(content);
		if(titleC){
			return 4;
		}
		return 1;
	}
	/**
	 * 验证Convene TITLE
	 * @return 1:正常；2字符太少；3字符太多;4含有非法字符
	 */
	public int validateConveneTitle(String title) {
		int min = Configuration.getIntegerValue(Configuration.CONVENE_TITLE_MIN_CHARS);
		int max = Configuration.getIntegerValue(Configuration.CONVENE_TITLE_MAX_CHARS);
		if(StringUtil.nullOrBlank(title)){
			return 2;
		}
		if(title.length() < min){
			return 2;
		}
		if(title.length() > max){
			return 3;
		}
		boolean titleC = FilterCharsUtil.containsFilterChars(title);
		if(titleC){
			return 4;
		}
		return 1;
	}
}
